package com.bonc.common.utils;

import com.asiainfo.uap.util.des.EncryptData;
import com.asiainfo.uap.util.des.UnEncryptData;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.StringTokenizer;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

/**
 *3des加解密工具类
 */
public class DES {

    public static String desEncryptData(String str) {
        EncryptData ed = new EncryptData();
        StringBuffer sb = new StringBuffer();
        String result = "";
        byte[] date_byte = str.getBytes();

        try {
            byte[] bss = ed.createEncryptData(date_byte, "DES");
            System.out.println("bss.length = " + bss.length);
            sb.append(bss.length + "|");

            for (int i = 0; i < bss.length; ++i) {
                sb.append(bss[i] + "|");
            }

            result = sb.toString();
            result = result.substring(0, result.length() - 1);
        } catch (InvalidKeyException var7) {
            var7.printStackTrace();
        } catch (IllegalStateException var8) {
            var8.printStackTrace();
        } catch (IllegalBlockSizeException var9) {
            var9.printStackTrace();
        } catch (BadPaddingException var10) {
            var10.printStackTrace();
        } catch (NoSuchPaddingException var11) {
            var11.printStackTrace();
        } catch (InvalidKeySpecException var12) {
            var12.printStackTrace();
        } catch (NoSuchAlgorithmException var13) {
            var13.printStackTrace();
        } catch (IllegalArgumentException var14) {
            var14.printStackTrace();
        } catch (SecurityException var15) {
            var15.printStackTrace();
        } catch (InstantiationException var16) {
            var16.printStackTrace();
        } catch (IllegalAccessException var17) {
            var17.printStackTrace();
        } catch (InvocationTargetException var18) {
            var18.printStackTrace();
        } catch (NoSuchMethodException var19) {
            var19.printStackTrace();
        } catch (ClassNotFoundException var20) {
            var20.printStackTrace();
        } catch (IOException var21) {
            var21.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        return result;
    }

    public static String desUnEncryptData(String str) {
        UnEncryptData ud = new UnEncryptData();
        String ss = "";

        try {
            int byte_i = Integer.parseInt(str.substring(0, str.indexOf("|")));
            byte[] date_byte = new byte[byte_i];
            str = str.substring(str.indexOf("|") + 1);
            StringTokenizer st = new StringTokenizer(str, "|");

            for (int i = 0; st.hasMoreTokens(); ++i) {
                byte_i = Integer.parseInt(st.nextToken());
                date_byte[i] = (byte) byte_i;
            }

            byte[] bss = ud.createUnEncryptData(date_byte, "DES");
            ss = new String(bss);
        } catch (Exception var8) {
            var8.printStackTrace();
        }

        return ss;
    }

    public static String desEncryptData1(String str) {
        EncryptData ed = new EncryptData();
        StringBuffer sb = new StringBuffer();
        String result = "";
        byte[] date_byte = str.getBytes();

        try {
            byte[] bss = ed.createEncryptData(date_byte, "DES");
            System.out.println("bss.length = " + bss.length);
            sb.append(bss.length + "%");

            for (int i = 0; i < bss.length; ++i) {
                sb.append(bss[i] + "%");
            }

            result = sb.toString();
            result = result.substring(0, result.length() - 1);
        } catch (InvalidKeyException var7) {
            var7.printStackTrace();
        } catch (IllegalStateException var8) {
            var8.printStackTrace();
        } catch (IllegalBlockSizeException var9) {
            var9.printStackTrace();
        } catch (BadPaddingException var10) {
            var10.printStackTrace();
        } catch (NoSuchPaddingException var11) {
            var11.printStackTrace();
        } catch (InvalidKeySpecException var12) {
            var12.printStackTrace();
        } catch (NoSuchAlgorithmException var13) {
            var13.printStackTrace();
        } catch (IllegalArgumentException var14) {
            var14.printStackTrace();
        } catch (SecurityException var15) {
            var15.printStackTrace();
        } catch (InstantiationException var16) {
            var16.printStackTrace();
        } catch (IllegalAccessException var17) {
            var17.printStackTrace();
        } catch (InvocationTargetException var18) {
            var18.printStackTrace();
        } catch (NoSuchMethodException var19) {
            var19.printStackTrace();
        } catch (ClassNotFoundException var20) {
            var20.printStackTrace();
        } catch (IOException var21) {
            var21.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        return result;
    }

    public static String desUnEncryptData1(String str) {
        UnEncryptData ud = new UnEncryptData();
        String ss = "";

        try {
            int byte_i = Integer.parseInt(str.substring(0, str.indexOf("%")));
            byte[] date_byte = new byte[byte_i];
            str = str.substring(str.indexOf("%") + 1);
            StringTokenizer st = new StringTokenizer(str, "%");

            for (int i = 0; st.hasMoreTokens(); ++i) {
                byte_i = Integer.parseInt(st.nextToken());
                date_byte[i] = (byte) byte_i;
            }

            byte[] bss = ud.createUnEncryptData(date_byte, "DES");
            ss = new String(bss);
        } catch (Exception var8) {
            var8.printStackTrace();
        }

        return ss;
    }


    public static void main(String[] args) {
        // 加密
        System.out.println(DES.desEncryptData("o)XQ)45aH-Db9a_"));
        // 解密
//        System.out.println(DES.desUnEncryptData("8|-82|4|-18|64|107|-7|50|-111"));

    }
}

